<?php

namespace register\model;

require_once("common/model/UserDAL.php");

class RegisterModel {

	const minUserLength = 3;
	const minPasswordLength = 4;

	/**
	 * @var \common\model\UserDAL $userDAL
	 */
	private $UserDAL;

	public function __construct(\common\model\BaseDAL $baseDAL) {
		$this->UserDAL = new \common\model\UserDAL($baseDAL);
	}

	/**
	 * @param  \common\model\User $user
	 * @return void
	 */
	public function register(\common\model\User $user) {		
		$this->validateUser($user);
		//if no exceptions are thrown, add user to database
		$this->UserDAL->addUser($user);
	}

	/**
	 * @param  User $user
	 * @return void
	 */
	public function validateUser(\common\model\User $user) {
		$password = $user->password;
		$username = $user->username;
		//check database if username is taken
		if ($this->UserDAL->userExists($user)) {
			throw new \Exception("User Already exist");
		}
		//if username is too short throw exception
		else if (strlen($username) < self::minUserLength) {
			throw new \Exception("Username is too short");	
		}
		//if password is too short throw exception
		else if (strlen($password) < self::minPasswordLength) {
			throw new \Exception("Password is too short");	
		}
	}	
}